package com.wac.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wac.blog.pojo.Say;
import com.wac.blog.service.SayService;
import com.wac.blog.utils.Utils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wac
 * @since 2020-08-04
 */
@Controller
@RequestMapping("/say")
public class SayController {
    @Autowired
    SayService sayService;

    @RequestMapping("/list")
    public String list(Model model){
        List<Say> sayList = sayService.list(null);
        model.addAttribute("sayList",sayList );
        return "page/say";
    }

    @RequestMapping("/write")
    public String write(Say say){
        Say saySave = sayService.getOne(new QueryWrapper<Say>().eq("id", say.getId()));
        if (saySave!=null) {
            saySave.setContent(say.getContent());
            saySave.setTitle(say.getTitle());
            say.setGmtCreate(Utils.getTime());
            sayService.updateById(saySave);
        } else {
            say.setId(Utils.getUuid());
            say.setGmtCreate(Utils.getTime());
            sayService.save(say);
        }
        return "redirect:/say/list";
    }

    @RequestMapping("/editor/{id}")
    public String toeditor(Model model,@PathVariable String id){
        Say say = sayService.getOne(new QueryWrapper<Say>().eq("id", id));
        model.addAttribute("say",say );
        List<Say> sayList = sayService.list(null);
        model.addAttribute("sayList",sayList );
        return "page/say";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable String id){
        Say say = sayService.getOne(new QueryWrapper<Say>().eq("id", id));
        sayService.removeById(say);
        return "redirect:/say/list";
    }
}

